const mongoose = require("mongoose")
const crypto = require("crypto") //引用加密模块
const fs = require("fs");
const path = require("path")
const UserSchema = new mongoose.Schema({
    loginName: String, //登录名
    pwd: String, //密码
    mobile: String, //手机号
    nickName: String, //姓名
    headUrl: String, //小程序头像路径
    wxid: String, //微信小程序id
    role: String, //当前用户角色  供应商 supplier  超级管理员 admin 宠主 petowners 门店 stores 用户 user
    state: String //用户当前的状态
}, {
        versionKey: false,
        timestamps: {
            createdAt: 'createTime',
            updatedAt: 'updateTime'
        } //自动管理修改时间
    })
const user = mongoose.model("User", UserSchema);
async function add() {
    const count = await user.countDocuments();
    if (count === 0) {
        const content = fs.readFileSync(path.join(__dirname, "./data/users.json")).toString();
        const users = JSON.parse(content)
        users.forEach(e => {
            const md5 = crypto.createHash("md5");
            e.pwd = md5.update(e.pwd).digest("hex")
        })
        await user.create(users)
    }
}
add();
module.exports = user;